package com.core.framework.web.easyweb;

import java.io.IOException;
import java.io.PrintWriter;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

import com.core.framework.tools.JsonResult;
import com.core.framework.tools.Tools;
import com.core.framework.tools.reflect.BeanReflectUtil;
import com.core.framework.web.easyweb.annotation.MethodType;
import com.core.framework.web.easyweb.context.ServletActionContext;
import com.core.log.Log;

public class EasyAction implements EasywebStatics{
    protected static org.slf4j.Logger logger     = org.slf4j.LoggerFactory.getLogger(EasyAction.class);
    protected Logger                  log        = Logger.getLogger(this.getClass());
    protected HttpServletRequest  request =ServletActionContext.getRequest();
    protected HttpServletResponse response=ServletActionContext.getResponse();
    protected JsonResult jsonResult = new JsonResult();
    private String methodName ;
    MethodType                        methodType = null;
    Method                            method     = null;
    public EasyAction()
    {
    	
    }
    public void init(String _methodName )
    {
    	methodName = _methodName;
        if (methodName == null || methodName.length() < 1) {
            methodName = "index"+expandFileName;
        }
        method = BeanReflectUtil.getMethod(this.getClass(), methodName);
        if (method == null) {
            if (log.isDebugEnabled())
                log.debug(" 没有发现该方法,请检查！");
            method = null;
            return;
        }
        else if (method.isAnnotationPresent(MethodType.class))
        {
            methodType = (MethodType) method.getAnnotation(MethodType.class);
            method.setAccessible(true);
        }
   
    }
    /**
     * 页面默认执行程序
     * @return
     */
    public String index()  {
        return "/index"+expandFileName;
    }
    public String[]  Process() throws IllegalArgumentException, IllegalAccessException, InvocationTargetException
    {
    		String[] pro = new String[2];
    		String path = null;
    		String type = null;
    		if(method==null)
    		{
    				//throw new RuntimeException("该类中没有发现该方法,请检测！");
    			path = index();
    		}
    		else 
    		{
    			if(methodType==null)
    			{
    				path  = run();
    			}
    			else if(MethodType.JSON.equals(methodType.value()))
    			{
    				path  = run();
    				type=methodType.value();
    				pushContent();
    			}
    			else if(MethodType.FORWARD.equals(methodType.value()))
    			{
    				path  = run();
    				type=methodType.value();
    			}
    			else if(MethodType.REDIRECT.equals(methodType.value()))
    			{
    				path  = run();
    				type=methodType.value();
    			}
    			else if(MethodType.NULL.equals(methodType.value()))
    			{
    				run();
    				type=methodType.value();
    			}
    			
    		}
    		
    		pro[0]=path;
    		pro[1]=type;
    		return pro;
    }
    public final  String run()
    {
    	 if (method != null) {
             method.setAccessible(true);
             Object reuslt=null;
 			try {
 				reuslt = method.invoke(this);
 			} catch (IllegalArgumentException e) {
 				Log.debug(e);
 			} catch (IllegalAccessException e) {
 				Log.debug(e);
 			} catch (InvocationTargetException e) {
 				e.getTargetException().printStackTrace();
 			}
             if (reuslt == null)
             {
                 return null;
             }
             return reuslt.toString();
         }
         return null;
    }
	private void pushContent()
	{
		String content = Tools.getGson().toJson(jsonResult);
		writeResponse(content);
	}
    protected void writeResponse(String content) {
        response.setContentType("text/html;charset=utf-8");
        PrintWriter writer = null;
        try {
            writer = response.getWriter();
        } catch (IOException e) {
            e.printStackTrace();
        }
        writer.print(content);
        writer.flush();
        writer.close();
    }
    public static void main()
    {
    	
    }
}
